﻿using FluentEmail.Core;
using FluentEmail.Core.Models;
using FluentEmail.Smtp;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Common.EmailHelper
{
    public class EmailHelper
    {
        private static string _host;//"smtp.qq.com";
        private static string _account;//"532050072@qq.com";
        private static string _password;//"hcfecasmmykjcbba";
        private static string _nickName;
        static EmailHelper()
        {
            var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
            //创建配置根对象
            var configurationRoot = builder.Build();
            var EmailConfig = configurationRoot.GetSection("EmailInfo");
            _host = EmailConfig["Host"];
            _account = EmailConfig["Account"];
            _password = EmailConfig["Password"];
            _nickName = EmailConfig["NickName"];
        }
        private static SmtpClient GetSmtp()
        {
            return new SmtpClient()
            {
                Host = _host,
                EnableSsl = true,//qq邮箱设置为true
                UseDefaultCredentials = false,//qq邮箱设置为false
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential(_account, _password),
            };
        }

        public static bool SendEmail(string title, string sendMessage, string reciveAddress, bool isHtml = false)
        {
            Email.DefaultSender = new SmtpSender(GetSmtp());
            var email = Email.From(_account, _nickName).To(reciveAddress).Subject(title).Body(sendMessage, isHtml);
            var result = email.Send();
            return result.Successful;
        }

        public static bool SendEmail(string title, string sendMessage, List<string> reciveAddress, bool isHtml = false)
        {
            Email.DefaultSender = new SmtpSender(GetSmtp());
            List<Address> toUsers = reciveAddress.Select(t => new Address() { EmailAddress = t }).ToList();
            var email = Email.From(_account, _nickName).To(toUsers).Subject(title).Body(sendMessage, isHtml);
            var result = email.Send();
            return result.Successful;
        }

        public static bool SendEmailByTemplateFile<T>(string title, T data, string reciveAddress,string templatePath)
        {
            Email.DefaultSender = new SmtpSender(GetSmtp());
            var email = Email.From(_account, _nickName).To(reciveAddress).Subject(title).UsingTemplateFromFile(templatePath,data);
            var result = email.Send();
            return result.Successful;
        }

        public static bool SendEmailByTemplate<T>(string title,string template,T data, string reciveAddress)
        {
            Email.DefaultSender = new SmtpSender(GetSmtp());
            var email = Email.From(_account, _nickName).To(reciveAddress).Subject(title).UsingTemplate(template, data);
            var result = email.Send();
            return result.Successful;
        }

        public static bool SendEmailByTemplate<T>(string title, string template, T data, List<string> reciveAddress)
        {
            List<Address> toUsers = reciveAddress.Select(t => new Address() { EmailAddress = t }).ToList();
            var email = Email.From(_account, _nickName).To(toUsers).Subject(title).UsingTemplate(template, data);
            var result = email.Send();
            return result.Successful;
        }


    }
}
